package com.vm.action.mobile;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.Page;
import com.vm.dao.mobile.base.IBusinessDao;
import com.vm.dao.mobile.base.ISmsDetailDao;
import com.vm.entity.Business;
import com.vm.log4j.CustomizeLog4j;

public class BusinessAction extends ActionSuper implements Preparable {
	
	private static Logger log = CustomizeLog4j.getLogger(BusinessAction.class);
	
	private Page<Business> page;
	private Business business;
	private IBusinessDao businessDao;
	
	private ISmsDetailDao smsDetailDao;
	private String pageLinkList;
	private Integer id;
	
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	
	public void prepare() throws Exception {
		//atuo
	}
	
	public String list(){
		this.page = this.businessDao.page(this.PAGE_ROW_SIZE, super.getPageNumber());
		if(null != page && null != page.getList()&& 0 < page.getList().size()){
//			for(Business business : page.getList()){
//				Integer count = 0; 
//				count = this.smsDetailDao.getSmsCountByBusinessId(business.getYwId());
//				business.setCount(count);
//			}
		}
		this.pageLinkList =super.getSplitPageLink("/mobile/BusinessAction_list.do?", this.page);
		return SUCCESS;
	}
	
	public String add() throws Exception {
		if(this.business != null){
			business = null;
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		String[] msgArg = new String[] { "BusinessAction_list.do"};
		String msg = null;
		try {
			String path = this.saveFile();
			business.setYwZsturl(path);
			
			businessDao.save(business);
			msg = super.getResourceMessage("insert_business_success", msgArg);
		} catch (Exception e) {
			log.error("BusinessAction save business error  ", e);
			msg = super.getResourceMessage("insert_business_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String edit(){
		business = businessDao.findById(this.getId());
		return SUCCESS;
	}

	public String update() throws Exception {
		String[] msgArg = new String[] { "BusinessAction_list.do"};
		String msg = null;
		try {
			String path = this.saveFile();
			business.setYwZsturl(path);
			
			businessDao.update(business);
			msg = super.getResourceMessage("update_business_success", msgArg);
		} catch (Exception e) {
			log.error("BusinessAction save business error  ", e);
			msg = super.getResourceMessage("update_business_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}

	public String delete() {
		String[] msgArg = new String[] { "BusinessAction_list.do"};
		String msg = null;
		try {
			businessDao.deleteById(this.getId());
			msg = super.getResourceMessage("delete_business_success", msgArg);
		} catch (Exception e) {
			log.error("BusinessAction delete business error  ", e);
			msg = super.getResourceMessage("delete_business_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	
	public String saveFile() throws Exception{
		String path = "";
		if(this.getUploadContentType() == null){
			return path;
		}
		
		String fileName = ServletActionContext.getRequest().getRealPath(savePath) + "\\" +getUploadFileName();
		FileOutputStream fos = new FileOutputStream(fileName);
		FileInputStream fis = new FileInputStream(this.getUpload());
		byte[] b = new byte[1024];
		int len = 0;
		while((len = fis.read(b)) > 0){
			fos.write(b, 0, len);
		}
		path = this.savePath+ File.separator +getUploadFileName();;
		return path;
		
	}

	public Page<Business> getPage() {
		return page;
	}

	public void setPage(Page<Business> page) {
		this.page = page;
	}

	public Business getBusiness() {
		return business;
	}

	public void setBusiness(Business business) {
		this.business = business;
	}

	public IBusinessDao getBusinessDao() {
		return businessDao;
	}

	public void setBusinessDao(IBusinessDao businessDao) {
		this.businessDao = businessDao;
	}

	public String getPageLinkList() {
		return pageLinkList;
	}

	public void setPageLinkList(String pageLinkList) {
		this.pageLinkList = pageLinkList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public ISmsDetailDao getSmsDetailDao() {
		return smsDetailDao;
	}

	public void setSmsDetailDao(ISmsDetailDao smsDetailDao) {
		this.smsDetailDao = smsDetailDao;
	}
	
}
